package com.example.viewpagerandlistview.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.viewpagerandlistview.Contents;
import com.example.viewpagerandlistview.R;

import java.util.List;

public class ContentAdapter extends BaseAdapter {

    Context context;
    List<Contents> list;

    public ContentAdapter(Context context, List<Contents> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = View.inflate(context, R.layout.item,null);
        }
        TextView mName = convertView.findViewById(R.id.tv_name);
        TextView mPhone = convertView.findViewById(R.id.tv_phone);
        ImageView mImage = convertView.findViewById(R.id.iv_img);
        mName.setText(list.get(position).getName());
        mPhone.setText(list.get(position).getPhone());
        mImage.setImageResource(context.getResources().getIdentifier(list.get(position).getImg(),"drawable",context.getPackageName()                   ));
        return convertView;
    }
}
